package com.xydtech.common.utils.rsa;

/**
 * 创建时间：2023/2/22 10:37
 * 项目名称：WechatSemApi
 * @author JHON
 * @version 1.0
 * @since JDK 1.8.0_201
 * 文件名称：RsaDecodeRequestBodyAdvice
 * 类说明：实现接口，定义接受参数的解密方法

 */


import com.xydtech.common.annotation.RsaRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;

@Slf4j
@ControllerAdvice(basePackages = {"com.xydtech.ecard", "com.xydtech.appapi"})
public class RsaDecodeRequestBodyAdvice implements RequestBodyAdvice {

    @Override
    public boolean supports(MethodParameter methodParameter, Type targetType,
                            Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter methodParameter, Type targetType,
                                           Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
        try {
            boolean param = false;
            if (methodParameter.getMethod().isAnnotationPresent(RsaRequest.class)) {
                //获取注解配置的包含和去除字段
                RsaRequest serializedField = methodParameter.getMethodAnnotation(RsaRequest.class);
                //入参是否需要解密
                param = serializedField.param();
            }
            if (param) {
                log.info("对参数进行进行解密");
                return new ResquestInputMessage(inputMessage);
            } else {
                return inputMessage;
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("对参数进行进行解密");
            return inputMessage;
        }
    }

    @Override
    public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
                                Class<? extends HttpMessageConverter<?>> converterType) {
        return body;
    }

    @Override
    public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
                                  Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return body;
    }

    class ResquestInputMessage implements HttpInputMessage {
        private HttpHeaders headers;
        private InputStream body;

        public ResquestInputMessage(HttpInputMessage inputMessage) throws Exception {
            this.headers = inputMessage.getHeaders();
            String content = IOUtils.toString(inputMessage.getBody(), "utf-8");
            this.body = IOUtils.toInputStream(RsaUtils.decryptLongByPrivateKey(content));
        }

        @Override
        public InputStream getBody() throws IOException {
            return body;
        }

        @Override
        public HttpHeaders getHeaders() {
            return headers;
        }

    }


}